import java.util.Random;
import java.util.Scanner;

public class Text {
    static final int DIGIT = 4;

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        Rule rule = new Rule(DIGIT);

        int[] number = new int[DIGIT];
        int[] guess = new int[DIGIT];
        int i = 0, j;
        int chance = 10, userInput = 0;
        int hit, blow;
        boolean isSame = false;

        //生成四位不重复的数字
        do {
            isSame = false;
            number[i] = rand.nextInt(10);
            for (j = 0; j < i; j++) {
                if (number[i] == number[j]) {
                    isSame = true;
                    break;
                }
            }
            if (isSame) continue;
            else i++;
        } while (i < DIGIT);

        while (true) {
            System.out.print("我已经想了一个数字，你猜它是几？");
            userInput = input.nextInt();
            if (userInput > 9999) {
                System.out.println("数位不对！应该输入四位数。");
                continue;
            }
            guess[0] = rule.getThousands(userInput);
            guess[1] = rule.getHundreds(userInput);
            guess[2] = rule.getTens(userInput);
            guess[3] = rule.getOnes(userInput);
            isSame = false;
            for (i = 0; i < DIGIT; i++) {
                for (j = 0; j < i; j++) {
                    if (guess[i] == guess[j]) {
                        System.out.println("不可以输入重复的数字！请重新输入。");
                        isSame = true;
                        break;
                    }
                }
                if (isSame) break;
            }
            if (isSame) continue;
            else chance--;

            hit = rule.hitCounter(number, guess);
            blow = rule.blowCounter(number, guess);
            System.out.printf("%dA%dB\n", hit, blow);

            if (hit == 4) {
                System.out.println("♬聪明☆\n\t绝顶♪\n\t恭喜你答对了");
                break;
            } else {
                if (chance == 0) {
                    System.out.println("白给了....答案：" + number[0] + number[1] + number[2] + number[3]);
                    System.exit(0);
                }
            }
        }
    }
}